/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase - Ex3
 * <p>Powered by webrx On 2021-12-27 09:47:45
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex3 {
    private Object a = new Object();
    private Object b = new Object();

    void m1() {
        String t = Thread.currentThread().getName();
        synchronized (a) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t + ": start");
            synchronized (b) {
                System.out.println(t + ": running");
            }
        }
        System.out.println(t + ":end");

    }

    void m2() {
        String t = Thread.currentThread().getName();
        synchronized (b) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t + ": start");
            synchronized (a) {
                System.out.println(t + ": running");
            }
        }
        System.out.println(t + ":end");
    }


    public static void main(String[] args) {
        var t = new Ex3();
        new Thread(t::m1, "A").start();
        new Thread(t::m2, "B").start();
    }
}
